/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.statusbar

import android.content.Context
import android.telephony.TelephonyManager
import android.telephony.TelephonyProtoEnums
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.ecarx.systemui.plugin.MobileSignalController
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.model.listener.MobileSignalCallBack
import com.ecarx.systemui.plugin.ui.custom.SystemBarButton
import com.ecarx.systemui.plugin.utils.LogUtils

class MobileSignalView : SystemBarButton, MobileSignalCallBack {

    private var mMobileSignalController: MobileSignalController? = null
    private var mNetworkType: Int = TelephonyProtoEnums.NETWORK_TYPE_UNKNOWN
    private var mNetworkState: Int = TelephonyManager.DATA_UNKNOWN
    val TWOG_RES_LIST = mutableListOf(
        R.drawable.ic_status_icon_signal_2g_0, R.drawable.ic_status_icon_signal_2g_1,
        R.drawable.ic_status_icon_signal_2g_2, R.drawable.ic_status_icon_signal_2g_3,
        R.drawable.ic_status_icon_signal_2g_4
    )

    val THREEG_RES_LIST = mutableListOf(
        R.drawable.ic_status_icon_signal_3g_0, R.drawable.ic_status_icon_signal_3g_1,
        R.drawable.ic_status_icon_signal_3g_2, R.drawable.ic_status_icon_signal_3g_3,
        R.drawable.ic_status_icon_signal_3g_4
    )

    val FOURG_RES_LIST = mutableListOf(
        R.drawable.ic_status_icon_signal_4g_0, R.drawable.ic_status_icon_signal_4g_1,
        R.drawable.ic_status_icon_signal_4g_2, R.drawable.ic_status_icon_signal_4g_3,
        R.drawable.ic_status_icon_signal_4g_4
    )

    val FIVEG_RES_LIST = mutableListOf(
        R.drawable.ic_status_icon_signal_5g_0, R.drawable.ic_status_icon_signal_5g_1,
        R.drawable.ic_status_icon_signal_5g_2, R.drawable.ic_status_icon_signal_5g_3,
        R.drawable.ic_status_icon_signal_5g_4
    )

    constructor(context: Context?) : this(context!!, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(context!!, attrs, 0) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        registerMobileSignalListener()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        unregisterMobileSignalListener()
    }

    private fun registerMobileSignalListener() {
        val mobileSignalController = MobileSignalController(context)
        mMobileSignalController = mobileSignalController
        mobileSignalController.setMobileSignalCallBack(this)
    }

    private fun unregisterMobileSignalListener() {
        mMobileSignalController?.destroyController(context)
    }

    override fun getContentDescriptionString(): String {
        return "MobileSignalView"
    }

    override fun initView() {
        super.initView()
        LogUtils.d(TAG, "initView")

        setImageDrawable(
            ResourcesCompat
                .getDrawable(
                    resources,
                    R.drawable.ic_status_icon_signal_unkonw,
                    context.theme
                )
        )

        setOnClickListener { v: View? ->

        }
    }

    override fun onSignalChanged(level: Int) {
        showMobileIndicator(level)
    }

    private fun showMobileIndicator(signal: Int) {
        setImageDrawable(
            ResourcesCompat
                .getDrawable(
                    resources,
                    getSignalResId(signal),
                    context.theme
                )
        )
    }

    private fun getSignalResId(signal: Int) =
        if (mNetworkState == TelephonyManager.DATA_CONNECTED && signal >= 0 && signal <= 4) {
            when (mNetworkType) {
                TelephonyProtoEnums.NETWORK_TYPE_GSM -> {
                    TWOG_RES_LIST[signal]
                }
                TelephonyProtoEnums.NETWORK_TYPE_UMTS -> {
                    THREEG_RES_LIST[signal]
                }
                TelephonyProtoEnums.NETWORK_TYPE_LTE -> {
                    FOURG_RES_LIST[signal]
                }
                TelephonyProtoEnums.NETWORK_TYPE_NR -> {
                    FIVEG_RES_LIST[signal]
                }
                else -> R.drawable.ic_status_icon_signal_unkonw
            }
        } else {
            R.drawable.ic_status_icon_signal_unkonw
        }

    override fun onNetworkStateChanged(state: Int, type: Int) {
        mNetworkState = state
        mNetworkType = type
        LogUtils.d(TAG, "mobile network state = $state， type = $type")
    }

    companion object {
        private const val TAG = "MobileSignalView"
    }

}